﻿using System;
using System.Data;
using System.Web;
using System.Data.SqlClient;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;

namespace AnyOffice.general.news.show
{
    public partial class query : ModulePage
    {
        public string GetNew(string readers)
        {
            readers = "," + readers + ",";
            if (readers.IndexOf("," + new UserCache().UserId + ",") < 0)
            {
                return "<img src=/images/new.gif align=absmiddle>";
            }
            return string.Empty;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new NewsTypeDao().SelectAll(base.Conn);
                this.sltTypeId.DataSource = table;
                this.sltTypeId.DataBind();
                this.sltTypeId.Items.Insert(0, new ListItem("全部", ""));
                this.txtSendTimeMIN.Value = string.Concat(new object[] { DateTime.Now.Year, "-", DateTime.Now.Month.ToString(), "-1" });
                this.litNavTitle.Text = "简报查询";
            }
            if (this.Page.IsPostBack)
            {
                this.litNavTitle.Text = "简报查询结果";
                this.pnlMain.Visible = false;
                this.pnlLater.Visible = true;
                SqlCommand command = new SqlCommand();
                base.Conn.Open();
                command.Connection = base.Conn;
                StringBuilder builder = new StringBuilder();
                builder.Append("SELECT ");
                builder.Append("\t\t[News].[NewsId], ");
                builder.Append("\t\t[News].[Subject], ");
                builder.Append("\t\t[News].[SendTime], ");
                builder.Append("\t\t[News].[Readers], ");
                builder.Append("\t\t[News].[ViewCount], ");
                builder.Append("\t\t[News_Type].[TypeName],[News].[TypeId], ");
                builder.Append("\t\t[Users].[UserName], ");
                builder.Append("\t\t[Department].[DeptName] ");
                builder.Append("\tFROM ");
                builder.Append("\t\t[dbo].[News], ");
                builder.Append("\t\t[dbo].[News_Type], ");
                builder.Append("\t\t[dbo].[Users], ");
                builder.Append("\t\t[dbo].[Department] ");
                builder.Append("\tWHERE ");
                builder.Append("\t\t[News].[TypeId] = [News_Type].[TypeId] ");
                if (this.txtSubject.Value != string.Empty)
                {
                    builder.Append(" AND [News].[Subject] LIKE '%' + @Subject + '%' ");
                    command.Parameters.Add("@Subject", this.txtSubject.Value);
                }
                if (this.sltTypeId.Value != string.Empty)
                {
                    builder.Append(" AND [News].[TypeId] =  @TypeId ");
                    command.Parameters.Add("@TypeId", Convert.ToInt32(this.sltTypeId.Value));
                }
                if (this.UserIds.Value != string.Empty)
                {
                    builder.Append(" AND CHARINDEX(',' + [News].[FromId] + ',',',' + CAST(@UserIds AS nvarchar(2000)) + ',') > 0 ");
                    command.Parameters.Add("@UserIds", this.UserIds.Value);
                }
                if (this.txtSendTimeMIN.Value != string.Empty)
                {
                    builder.Append(" AND [News].[SendTime] >= @MinDate ");
                    command.Parameters.Add("@MinDate", Convert.ToDateTime(this.txtSendTimeMIN.Value + " 00:00:00"));
                }
                if (this.txtSendTimeMAX.Value != string.Empty)
                {
                    builder.Append(" AND [News].[SendTime] <= @MaxDate ");
                    command.Parameters.Add("@MaxDate", Convert.ToDateTime(this.txtSendTimeMAX.Value + " 23:59:59"));
                }
                if (this.txtContent.Value != string.Empty)
                {
                    builder.Append(" AND [News].[Content] LIKE '%' + @Content + '%' ");
                    command.Parameters.Add("@Content", this.txtContent.Value);
                }
                builder.Append("\t\tAND [News].[FromId] = [Users].[UserId] ");
                builder.Append("\t\tAND [Users].[DeptId] = [Department].[DeptId] ");
                builder.Append("\t\tAND ( ");
                builder.Append("\t\t\tCAST([ToId] AS nvarchar(20)) = 'ALL_DEPT' ");
                builder.Append("\t\t\tOR (CHARINDEX(',' + CAST(@DeptId AS nvarchar(20)) + ',',',' + CAST([ToId] AS nvarchar(2000)) + ',') > 0) ");
                builder.Append("\t\t\tOR (CHARINDEX(',' + CAST(@PrivId AS nvarchar(20)) + ',',',' + CAST([ToPriv] AS nvarchar(2000)) + ',') > 0) ");
                builder.Append("\t\t\tOR (CHARINDEX(',' + @UserId + ',',',' + CAST([ToUser] AS nvarchar(4000)) + ',') > 0) ");
                builder.Append("\t\t    ) ");
                builder.Append("\tORDER BY ");
                builder.Append("\t\t[News].[NewsId] DESC ");
                UserCache cache = new UserCache();
                command.Parameters.Add("@DeptId", cache.DeptId);
                command.Parameters.Add("@PrivId", cache.PrivId);
                command.Parameters.Add("@UserId", base.User.Identity.Name);
                command.CommandText = builder.ToString();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    this.rptNews.DataSource = reader;
                    this.rptNews.DataBind();
                }
                else
                {
                    this.litMessage.Text = base.ShowMessage("无符合条件的简报");
                }
            }
        }

 

 

    }
}
